/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.workflow.container;

import java.util.HashMap;
import java.util.Map;

import cc.aileron.workflow.container.tree.WorkflowTree;
import cc.aileron.workflow.container.tree.WorkflowTreeContainer;
import cc.aileron.workflow.container.tree.WorkflowTreeContainerResult;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * 実装
 * 
 * @author Aileron
 */
@Singleton
public class WorkflowContainerImpl implements WorkflowContainer
{
    @Override
    public Map<Integer, WorkflowContainerMaintenanceData> all()
    {
        return maintenanceMap;
    }

    @Override
    public WorkflowDto get(final WorkflowFindCondition c)
    {
        final WorkflowTreeContainerResult result = container.get(c.uri,
                c.method, c.parameter.getKeys());
        if (result == null)
        {
            return WorkflowDto.nullObject;
        }

        final HashMap<String, Object> parameters = new HashMap<String, Object>();
        for (final String key : c.parameter.getKeys())
        {
            parameters.put(key, c.parameter.get(key));
        }
        parameters.putAll(result.uriParameters);
        return new WorkflowDto(result.isThrough, parameters, map.get(result.id));
    }

    @Override
    public void put(final WorkflowRegistryCondition c,
            final WorkflowExecutor executor)
    {
        final WorkflowTree tree = new WorkflowTree();
        tree.isThrough = c.isThrough;
        tree.method = c.method;
        tree.overrideKey = c.overrideKey;
        tree.uri = c.uri;
        container.put(tree);
        map.put(tree.id, executor);

        final WorkflowContainerMaintenanceData data = new WorkflowContainerMaintenanceData();
        data.executor = executor;
        data.tree = tree;
        maintenanceMap.put(tree.id, data);
    }

    /**
     * constractor injection
     * 
     * @param container
     */
    @Inject
    public WorkflowContainerImpl(final WorkflowTreeContainer container)
    {
        this.container = container;
    }

    /**
     * container
     */
    private final WorkflowTreeContainer container;

    /**
     * メンテナンス用
     */
    private final HashMap<Integer, WorkflowContainerMaintenanceData> maintenanceMap = new HashMap<Integer, WorkflowContainerMaintenanceData>();

    /**
     * map
     */
    private final HashMap<Integer, WorkflowExecutor> map = new HashMap<Integer, WorkflowExecutor>();
}
